package com.yanqu.road.dao.impl.activity.witchdraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.witchdraw.config.WitchDrawCombConfig;
import com.yanqu.road.entity.activity.witchdraw.config.WitchDrawItemConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WitchDrawConfigDaoImpl extends TempDao {

    private static String getWitchDrawCombConfigSql = "select * from `t_s_activity_witch_comb` where `activityId`=?";
    public Map<Integer,WitchDrawCombConfig> getWitchDrawCombConfigMap(int activityId) {
        Map<Integer,WitchDrawCombConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWitchDrawCombConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WitchDrawCombConfig witchDrawCombConfig = new WitchDrawCombConfig();
                witchDrawCombConfig.setActivityId(rs.getInt("activityId"));
                witchDrawCombConfig.setId(rs.getInt("id"));
                witchDrawCombConfig.setType(rs.getInt("type"));
                witchDrawCombConfig.setCombParam(rs.getString("combParam"));
                witchDrawCombConfig.setReward(rs.getString("reward"));
                witchDrawCombConfig.setWeight(rs.getInt("weight"));
                witchDrawCombConfig.setShowWeight(rs.getInt("showWeight"));
                witchDrawCombConfig.setFloatWeight(rs.getString("floatWeight"));
                map.put(witchDrawCombConfig.getId(),witchDrawCombConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getWitchDrawItemConfigSql = "select * from `t_s_activity_witch_item` where `activityId`=?";
    public Map<Integer,WitchDrawItemConfig> getWitchDrawItemConfigMap(int activityId) {
        Map<Integer,WitchDrawItemConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getWitchDrawItemConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WitchDrawItemConfig witchDrawItemConfig = new WitchDrawItemConfig();
                witchDrawItemConfig.setActivityId(rs.getInt("activityId"));
                witchDrawItemConfig.setId(rs.getInt("id"));
                witchDrawItemConfig.setName(rs.getString("name"));
                witchDrawItemConfig.setDesc(rs.getString("desc"));
                map.put(witchDrawItemConfig.getId(),witchDrawItemConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }




}
